package PWS;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class PixelChanger {
	
	String IN;
	String OUT;
	String[] KEYS;

	public PixelChanger(String path_in, String path_out, String[] keys) {
		this.IN = path_in;
		this.OUT = path_out;
		this.KEYS = keys;
	}

	public void encrypt() {
		try {
			BufferedImage in = ImageIO.read(new File(IN));
			BufferedImage out = new BufferedImage(in.getWidth(),
					in.getHeight(), BufferedImage.TYPE_INT_ARGB);

			for (int x = 0; x < in.getWidth(); x++) {
				for (int y = 0; y < in.getHeight(); y++) {
					out.setRGB(x, y, in.getRGB(x, y)
							^ hex2decimal(KEYS[(x * y) % KEYS.length]));
				}
			}

			ImageIO.write(out, OUT.substring(OUT.length() - 3, OUT.length()),
					new File(OUT));
		} catch (IOException e) {
		}
	}
	
	public void decrypt() {
		encrypt();
	}
	
	public static int hex2decimal(String s) {
        String digits = "0123456789ABCDEF";
        s = s.toUpperCase();
        int val = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            int d = digits.indexOf(c);
            val = 16*val + d;
        }
        return val;
    }

}
